package cn.yao.jobclient.config;

import com.netflix.loadbalancer.BestAvailableRule;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RoundRobinRule;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;


@Configuration
public class RestTemplateConfig {


    /**
     *  带有 Ribbon 客户端负载 功能的RestTemplate
     * @param builder
     * @return
     */

    @Bean
    @LoadBalanced
    public RestTemplate createRest(RestTemplateBuilder builder){
        return builder.setConnectTimeout(10000).setReadTimeout(10000).build();
    }

    @Bean
    public IRule myRule(){
        return new RoundRobinRule();//轮询
//        return new RandomRule();//随机策略
        //return new RetryRule();//重试
//        return new BestAvailableRule();//最大可用策略
    }

}
